package custom

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/widget"
	"image/color"
	"log"
)

type TestWidget struct {
	*widget.BaseWidget
	X float32
	Y float32
}

func NewTestWidget(size fyne.Size) *TestWidget {
	o := &TestWidget{
		BaseWidget: &widget.BaseWidget{},
	}
	o.BaseWidget.Resize(size)
	return o
}

func (w *TestWidget) CreateRenderer() fyne.WidgetRenderer {
	return TestWidgetRenderer{w}
}

func (w *TestWidget) MinSize() fyne.Size {
	return fyne.NewSize(400, 200)
}

// MouseIn is a hook that is called if the mouse pointer enters the element.
func (w *TestWidget) MouseIn(event *desktop.MouseEvent) {
}

// MouseMoved is a hook that is called if the mouse pointer moved over the element.
func (w *TestWidget) MouseMoved(event *desktop.MouseEvent) {
	w.X, w.Y = event.Position.X, event.Position.Y
	log.Printf("MouseMoved %v  %v", w.X, w.Y)
	w.Refresh()
}

// MouseOut is a hook that is called if the mouse pointer leaves the element.
func (w *TestWidget) MouseOut() {

}

func (w *TestWidget) MouseDown(*desktop.MouseEvent) {
	log.Println("down")
}
func (w *TestWidget) MouseUp(*desktop.MouseEvent) {
	log.Println("up")
}

type TestWidgetRenderer struct {
	w *TestWidget
}

// Destroy is for internal use.
func (r TestWidgetRenderer) Destroy() {
}

func (r TestWidgetRenderer) MinSize() fyne.Size {
	return fyne.Size{400, 200}
}

// Layout is a hook that is called if the widget needs to be laid out.
// This should never call Refresh.
func (r TestWidgetRenderer) Layout(size fyne.Size) {
}

// Objects returns all objects that should be drawn.
func (r TestWidgetRenderer) Objects() []fyne.CanvasObject {
	res := make([]fyne.CanvasObject, 0)
	res = append(res, &canvas.Line{
		Position1:   fyne.Position{0, 0},
		Position2:   fyne.Position{r.w.X, r.w.Y},
		Hidden:      false,
		StrokeColor: color.Black,
		StrokeWidth: 5,
	})
	return res
}

// Refresh is a hook that is called if the widget has updated and needs to be redrawn.
// This might trigger a Layout.
func (r TestWidgetRenderer) Refresh() {
}
